package com.baomidou.ant.controller;

import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.BagContent;
import com.baomidou.ant.entity.PointsStore;
import com.baomidou.ant.entity.UserBag;
import com.baomidou.ant.service.IBagContentService;
import com.baomidou.ant.service.IPointsStoreService;
import com.baomidou.ant.service.IUserBagService;
import com.baomidou.ant.vo.Request.RequestUseGoodsVo;
import com.baomidou.ant.vo.Response.ResponseMyBagListVo;
import com.baomidou.ant.vo.Response.ResponseMyPropsVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-03-24
 */
@RestController
@RequestMapping("/bag-content")
public class BagContentController {
    @Autowired
    private IBagContentService bagContentService;
    @Autowired
    private IPointsStoreService pointsStoreService;
    @Autowired
    private IUserBagService userBagService;

    //背包内容物导出——————根据兑换清单导出背包物品
    @GetMapping("/listMyBag/{userId}")
    public Result listMyBag(@PathVariable Integer userId){
        List<ResponseMyBagListVo> voList=bagContentService.listMyBag(userId);
        return Result.getSuccess().setData(voList);
    }

    //使用背包中物品接口
    @PostMapping("/useGoods")
    public Result useGoods(@RequestBody RequestUseGoodsVo requestUseGoodsVo){
        String result=bagContentService.useGoods(requestUseGoodsVo);
        if(Objects.equals(result, "饰品") || Objects.equals(result, "addBag")||Objects.equals(result, "giftPackage")){
            return Result.getSuccess().setMsg("使用成功!");
        }
        if(Objects.equals(result, "propsOfLevel")){
            return Result.getFailure().setMsg("关卡道具请在关卡中使用！");
        }
        if(Objects.equals(result, "propOfName")){
            return Result.getFailure().setMsg("改名卡下次再用");
        }
        if(Objects.equals(result,"remake")){
            return Result.getSuccess().setMsg("您的无尽纪录已经恢复，快去继续挑战吧");
        }
        if(Objects.equals(result,"errorRemake")){
            return Result.getFailure().setMsg("您无需复活!");
        }
        if(Objects.equals(result,"musicPlayer")){
            return Result.getSuccess().setMsg("播放器已激活");
        }
        if(Objects.equals(result,"musicCollection")){
            return Result.getSuccess().setMsg("coming soon");
        }
        if(Objects.equals(result,"music")){
            return Result.getSuccess().setMsg("您的唱片机添加了新唱片");
        }
        return Result.getSuccess();
    }

    //丢弃背包物品接口
    @PostMapping("/removeGoods")
    public Result removeGoods(@RequestBody RequestUseGoodsVo vo){
        PointsStore goods=pointsStoreService.getById(vo.getGoodsId());
        if(Objects.equals(goods.getIsQuality(), "3") ||Objects.equals(goods.getGoodsType(), "饰品")){
            return Result.getFailure().setMsg("该物品不能丢弃!");
        }
        QueryWrapper<BagContent> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("goods_id",vo.getGoodsId());
        queryWrapper.eq("user_id",vo.getUserId());
        BagContent bagContent=bagContentService.getOne(queryWrapper);
        //数量-1
        bagContent.setAmount(bagContent.getAmount()-1);
        bagContentService.saveOrUpdate(bagContent);
        if(bagContent.getAmount()==0){
            //删除字段
            /*bagContentService.removeById(bagContent.getId());*/
            //背包更新
            QueryWrapper<UserBag> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.eq("user_id",vo.getUserId());
            UserBag userBag=userBagService.getOne(queryWrapper1);
            userBag.setUsedAmount(userBag.getUsedAmount()-1);
            userBagService.saveOrUpdate(userBag);
        }
        return Result.getSuccess().setMsg("丢弃成功");
    }

    //关卡道具展示
    @GetMapping("/listMyProps/{userId}")
    public Result listMyProps(@PathVariable Integer userId){
        List<ResponseMyPropsVo> vo=bagContentService.myProps(userId);
        return Result.getSuccess().setData(vo);
    }

    //关卡道具使用
    @PostMapping("/usePropsOfLevel")
    public Result usePropsOfLevel(@RequestBody RequestUseGoodsVo vo){
        String result=bagContentService.useProps(vo);
        return Result.getSuccess().setMsg(result);
    }

}
